package com.peach.SSMDemo.dao;

import com.peach.SSMDemo.domain.FltRecord;
import com.peach.SSMDemo.domain.FltRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

import com.peach.SSMDemo.domain.Flt;
import org.springframework.stereotype.Repository;

/**
 * Created by tao on 2017/8/29.
 */
@Repository
public class FltDao {

    public SqlSession sessionConfigure () {
        //mybatis的配置文件*********************
        String resource = "jdbcConfiguration.xml";
        //使用类加载器加载mybatis的配置文件（它也加载关联的映射文件）
        InputStream is = FltDao.class.getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用MyBatis提供的Resources类加载mybatis的配置文件（它也加载关联的映射文件）
        //Reader reader = Resources.getResourceAsReader(resource);
        //构建sqlSession的工厂
        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        return session;
    }

    public List<FltRecord> getAllWithDate (@Param(value = "fltRequest") FltRequest fltRequest){

        SqlSession session = this.sessionConfigure();
        //Mapper.xml中对应的sql语句id ********************
        String statement = "fltMapper.selectRecordWithDate";
        //******************传入参数

        List<FltRecord> fltRecords = session.selectList(statement,fltRequest);

//        System.out.println(fltRequest.getStartDate()+ "---" + "---" +fltRequest.getEndtDate());

        return fltRecords;
    }



}
